package org.zch.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.zch.common.ApiResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.zch.entity.po.GoodsPO;
import org.zch.service.GoodsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 商品表(Goods)表控制层
 *
 * @author 刘新
 * @since 2024-01-09 10:59:19
 */
@RestController
@RequestMapping("goods")
@Api(tags = "商品")
public class GoodsController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsService goodsService;

    /**
     * 分页查询所有数据
     *
     * @param page  分页对象
     * @param goodsPO 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询所有数据", notes = "分页查询所有数据")
    @PostMapping("/getPage")
    public ApiResult selectAll(Page<GoodsPO> page, GoodsPO goodsPO) {
        LambdaQueryWrapper<GoodsPO> goodsPOQueryWrapper = new LambdaQueryWrapper<>(goodsPO);
        goodsPOQueryWrapper.orderByDesc(GoodsPO::getCreateTime);
        return ApiResult.ok(this.goodsService.page(page, goodsPOQueryWrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据", notes = "通过主键查询单条数据")
    @GetMapping("/getById")
    public ApiResult getById(@PathVariable Serializable id) {
        return ApiResult.ok(this.goodsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param goodsPO 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping("/insert")
    public ApiResult insert(@RequestBody GoodsPO goodsPO) {
        goodsPO.setCreateTime(new Date());
        return ApiResult.ok(this.goodsService.save(goodsPO));
    }

    /**
     * 修改数据
     *
     * @param goodsPO 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改数据", notes = "修改数据")
    @PutMapping("/update")
    public ApiResult update(@RequestBody GoodsPO goodsPO) {
        return ApiResult.ok(this.goodsService.updateById(goodsPO));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/deleteByIds")
    public ApiResult delete(@RequestBody List<Integer> idList) {
        return ApiResult.ok(this.goodsService.removeByIds(idList));
    }
}

